package com.wms.controller;

import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.minio.MinioPojo;
import com.wms.minio.MinioService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "minio文件管理")
public class MinioController {
    @Autowired
    MinioService minioService;
    @Autowired
    private MinioPojo minio;


    @ApiOperation("文件上传")
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultBean upload(@RequestParam("file") MultipartFile file) {
        String url =minioService.upload(minio.getBucketName(), file);
        return R.success("文件上传", url);
    }

    @ApiOperation("文件下载")
    @GetMapping("/download/{url}")
    public ResponseEntity<InputStreamResource>
    download(@PathVariable String url) throws
            Exception {
        ResponseEntity<InputStreamResource>
                download = minioService.download(url);
        return download;
    }

    @ApiOperation("文件删除")
    @DeleteMapping("/delete/{url}")
    public ResultBean delete(@PathVariable
                             String url) {
        String delete =minioService.delete(url);
        return R.success("文件删除成功",
                delete);
    }
}